###############################################################################
#
# Copyright (c) 2011 Rainmaker Entertainment
# All Rights Reserved.
#
# This file contains unpublished confidential and proprietary
# information of Rainmaker Entertainment.  The contents of this file
# may not be copied or duplicated, in whole or in part, by any
# means, electronic or hardcopy, without the express prior
# written permission of Rainmaker Entertainment.
#
#    $HeadURL: http://pt/src/trunk/maya/rnkAssetChecks/rig.py $
#    $Revision: 65467 $
#    $Author: nkoubi $
#    $Date: 2013-06-27 10:49:13 -0700 (Thu, 27 Jun 2013) $
#
###############################################################################

###############################################################################
## INFOS
###############################################################################

# Rig checks


###############################################################################
## MODULES
###############################################################################

import traceback
import re

try :
    import maya.cmds as cmds
    import maya.mel as mel
    import maya.OpenMaya as OpenMaya
except :
    traceback.print_exc()
    print "Can't load Maya modules."
    
    
import rnkAssetChecks.core as rnkAssetChecksCore
reload(rnkAssetChecksCore)
        


###############################################################################
## FUNCTIONS
###############################################################################  


def checkNodesNotInContainers(assetRoot):
    """ 
    @brief Check to ensure that all nodes are inside a dagContainer.
    @return : List
    
    @param assetRoot - String - Asset root node.
    """
        
    getAllInContainers = []
    getAllNotInContainers = []
    
    ## Get all dg nodes in the last containers
    containersList = cmds.listRelatives(assetRoot, allDescendents=True, type='dagContainer')
    if not containersList is None :
            for eachContainer in containersList :
                    if cmds.listRelatives(eachContainer, children=True, type='dagContainer') is None :
                            getNodesInContainer = cmds.listRelatives(eachContainer, allDescendents=True)
                            if getNodesInContainer is not None :
                                    getAllInContainers.extend(getNodesInContainer[0:])
                 
    
    
            ## Get all DG nodes outside the containers         
            getAllDg = cmds.listRelatives(assetRoot, allDescendents=True)
            getAllDg.remove(cmds.listRelatives(assetRoot, type='shape')[0])
            for eachContainer in getAllInContainers :
                getAllDg.remove(eachContainer)
            
            getAllNotInContainers = []
            for eachNode in getAllDg :
                    if not eachNode in containersList :
                        getAllNotInContainers.append(eachNode)
                    
                    
    return getAllNotInContainers
    
    
def checkNonNormalizedWeights():
    """ 
    @brief Check to ensure that all weights are normalized.
    @return : List
    """
    
    ## Non-Normalize list
    nonNormalizeList = []
    
    ## Get all skinClusters
    getAllSkinClusters = cmds.ls(type='skinCluster')
    if not len(getAllSkinClusters) is 0 :
            for eachSkin in getAllSkinClusters :
                    ## Get connected geometry
                    getConnectedGeoShape = cmds.skinCluster(eachSkin, q=True, geometry=True)
                    if not getConnectedGeoShape is None :
                            getConnectedGeoShape = getConnectedGeoShape[0]
                            getConnectedGeo = cmds.listRelatives(getConnectedGeoShape, parent=True)[0]
                            
                            ## Get number of vtx
                            if cmds.nodeType(getConnectedGeoShape) == 'mesh' :
                                getNumVtx = cmds.polyEvaluate(getConnectedGeo, vertex=True)
                                if str(getNumVtx).isdigit() is True :
                                        ## Check if the sum of weights is 1.0
                                        for eachVtx in range(getNumVtx) :
                                                getWeightVal = cmds.skinPercent(eachSkin, '%s.vtx[%s]' % (getConnectedGeo, eachVtx), q=True, value=True)
                                                if getWeightVal is None :
                                                        nonNormalizeList.append('%s.vtx[%s]' % (getConnectedGeo, eachVtx))
                                                else :
                                                        if not float('%.1f' % sum(getWeightVal)) == 1.0 :
                                                                nonNormalizeList.append('%s.vtx[%s]' % (getConnectedGeo, eachVtx))
         
    return nonNormalizeList    
    
    
    
def checkNanErrors():
    """ 
    @brief Check to ensure that all skinCluster weights are valid.
    @return : List
    """
    
    nanList = []
    
    getSkinCluster = cmds.ls(type='skinCluster')
    if not len(getSkinCluster) == 0 :
            for eachSkin in getSkinCluster :
                    for eachVal in cmds.getAttr('%s.bw' % eachSkin)[0] :
                            if eachVal == 0.0 and not eachSkin in nanList :
                                    nanList.append(eachSkin)
                                        
    return nanList                                    